<template>
  <div class="artcle-content">
    <el-card class="box-card">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>内容管理</el-breadcrumb-item>
      </el-breadcrumb>

      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="特殊资源">
          <el-radio-group v-model="status">
            <!-- el-radio 默认把label 作为文本和选中之后的value值 -->
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道">
          <el-select
          v-model="channelId"
          placeholder="请选择频道"
          @change="getArtcle2"
          >
             <el-option
            label="全部"
            :value="null"></el-option>
            <el-option
            v-for="(item,index) in channels"
            :key="index"
            :label="item.name"
            :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期">
          <el-date-picker
            v-model="rangDate"
            type="datetimerange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['12:00:00']"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <!-- button 按钮的click事件有个默认参数
          当没有指定参数时，会默认传递一个没用的数据 -->
          <el-button
          type="primary"
          :disabled='loading'
          @click="loadArticles(status)"> 查询 </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <div class="clearfix" slot="header">
        根据筛选条件共查询到{{ totalCount }}条结果；
      </div>
      <!-- prop只能展示文本
      表格列默认只能渲染普通文本，如果需要展示其他内容，就需要自定义一个表格列模板
       -->
      <el-table
      class="list-table"
      :data="articles"
      stripe style="width: 100%"
      v-loading='loading'
      >
        <el-table-column label="封面" width="180">
          <template slot-scope="scope">
            <img
              class="article-cover"
             :src="scope.row.cover.images[0]" 
             alt="">
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="180">
        </el-table-column>
        <el-table-column label="状态" width="180">
          <!-- 如果需要在自定义列魔板中获取当前遍历项数据，那么就在template上声明
          slot-scope="scope"
           -->
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status==0">草稿</el-tag>
            <el-tag v-else-if="scope.row.status==2"  type="success">审核通过</el-tag>
            <el-tag v-else-if="scope.row.status==1"  type="info">待审核</el-tag>
            <el-tag v-else-if="scope.row.status==4"  type="warning">已删除</el-tag>
            <el-tag v-else-if="scope.row.status==3"  type="danger">审核失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间" width="180">
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              circle
              @click="onDelete(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- total 用来设定总数据的条数 -->
      <el-pagination
      background
      layout="prev, pager, next"
      :total="totalCount"
      :page-size="pageSize"
      :disabled='loading'
      @current-change="onChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArtcle,getChannels,deleteArticle } from "@/api/article";

export default {
  data() {
    return {
      articles: [],
      channels: [], // 文章频道列表
      totalCount: 0,
      pageSize:20, // 每页显示条数
      status: null, // 查询文章状态
      channelId: null, // 查询文章的频道
      rangDate: [],
      loading: true,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: "",
      },
    };
  },
  created() {
    this.loadArticles(1);
    this.loadChannels();
  },
  methods: {
    loadArticles(page = 1) {
      this.loading=true
      getArtcle({
        page,
        per_page: this.pageSize,
        status: this.status,
        channel_id: this.channelId,
        begin_pubdate: this.rangDate[0] ? this.rangDate[0] : null,
        end_pubdate: this.rangDate[1] ? this.rangDate[1] : null
      }).then((res) => {
        this.articles = res.data.data.results;
        this.totalCount = res.data.data.total_count;
        this.loading=false
      });
      
    },
    loadChannels(){
       getChannels({

       }).then((res)=>{
        this.channels = res.data.data.channels;
       })
    },
    getArtcle2(){
      this.loadArticles(1);
    },
    onChange(page) {
     this.loadArticles(page)
    },
    onDelete(articleId){
     
      this.$confirm('确认删除该文章?', '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteArticle(articleId.toString()).then(res => {

          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  },
};
</script>

<style lang="less" scoped>
.box-card {
  margin-bottom: 30px;
}
.list-table {
  margin-bottom: 20px;
  .article-cover{
    width: 100px;
    // height: 100px;
    background-size: cover;
  }
}
</style>>
